{% extends 'public/layout.html' %}


{% block load_css %}
    <link href="/static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
{% endblock %}

{% block mbx %}
    <div class="row wrapper border-bottom white-bg page-heading">
        <div class="col-sm-10">
            <h2></h2>
            <ol class="breadcrumb">
                <li>
                    <a href="{% url 'index' %}">仪表盘</a>
                </li>

                <li>
                    <a>用户管理</a>
                </li>


                <li class="active">
                    <strong>用户列表</strong>
                </li>

            </ol>
        </div>
        <div class="col-sm-2">
        </div>
    </div>
{% endblock %}

{% block body %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>用户列表</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                            <a class="close-link">
                                <i class="fa fa-times"></i>
                            </a>
                        </div>
                    </div>
                    <div class="ibox-content">
                        {% if perms.users.add_user %}
                            <a href="{% url 'user_create' %}" class="btn btn-sm btn-primary"> 创建用户 </a>
                        {% endif %}
                        <table class="table table-striped table-bordered table-hover  dataTable no-footer">
                            <thead>
                            <tr>
                                <th class="text-center">#</th>
                                <th class="text-center">中文名</th>
                                <th class="text-center">用户名</th>
                                <th class="text-center">邮箱</th>
                                <th class="text-center">手机号</th>
                                <th class="text-center">微信号</th>
                                <th class="text-center">激活状态</th>
                                {% if perms.users.modify_user or perms.users.user_status_stop or perms.users.delete_user or perms.users.user_set_password or perms.users.user_set_perm %}
                                    <th class="text-center">操作</th>
                                {% endif %}
                            </tr>
                            </thead>
                            <tbody>
                            {% for user_list in object_list %}
                                <tr>
                                    <td class="text-center">{{ forloop.counter }}</td>
                                    <td class="text-center"><a
                                            href="{% url 'user_detail' user_list.user.id %}">{{ user_list.name }}</a>
                                    </td>
                                    <td class="text-center">{{ user_list.user.username }}</td>
                                    <td class="text-center">{{ user_list.user.email }}</td>
                                    <td class="text-center">{{ user_list.phone }}</td>
                                    <td class="text-center">{{ user_list.weixin }}</td>
                                    <td class="text-center">
                                        {% if user_list.user.is_active %}
                                            <i class="fa fa-check text-navy"></i>
                                        {% else %}
                                            <i class="fa fa-times text-danger"></i>
                                        {% endif %}
                                    </td>
                                    {% if perms.users.modify_user or perms.users.user_status_stop or perms.users.delete_user or perms.users.user_set_password or perms.users.user_set_perm %}
                                        <td class="text-center">
                                            {% if perms.users.modify_user %}
                                                <a class="btn btn-xs btn-info"
                                                   href="{% url 'user_modify' %}?uid={{ user_list.user.id }}">更新</a>
                                            {% endif %}
                                            {% if perms.users.user_status_stop %}
                                                {% if user_list.user.is_active %}
                                                    <a class="btn btn-xs btn-warning" id="user_stop"
                                                       onclick="user_stop({{ user_list.user.id }})">禁用</a>
                                                {% else %}
                                                    <a class="btn btn-xs btn-primary" id="user_stop"
                                                       onclick="user_start({{ user_list.user.id }})">启用</a>
                                                {% endif %}
                                            {% endif %}
                                            {% if perms.users.delete_user %}
                                                <a class="btn btn-xs btn-danger" id="user_delete"
                                                   onclick="user_delete({{ user_list.user.id }})">删除</a>
                                            {% endif %}
                                            {% if perms.users.user_set_password %}
                                                <button type="button" class="btn btn-xs btn-success" data-toggle="modal"
                                                        data-target="#myModal{{ user_list.user.id }}">
                                                    设置密码
                                                </button>
                                            {% endif %}
                                        </td>
                                    {% endif %}
                                </tr>

                                <div class="modal inmodal fade" id="myModal{{ user_list.user.id }}" tabindex="-1"
                                     role="dialog"
                                     aria-hidden="true">
                                    <div class="modal-dialog modal-sm">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal"><span
                                                        aria-hidden="true">&times;</span><span
                                                        class="sr-only">Close</span>
                                                </button>
                                                <h4 class="modal-title">设置{{ user_list.user.username }}密码</h4>
                                            </div>
                                            <div class="modal-body">

                                                <div class="form-group">
                                                    <input type="password" class="form-control" placeholder="密码"
                                                           id="pass_{{ user_list.user.id }}_1">
                                                </div>

                                                <div class="form-group">
                                                    <input type="password" class="form-control" placeholder="第二次密码"
                                                           id="pass_{{ user_list.user.id }}_2">
                                                </div>

                                                <div class="form-group">
                                                    <input type="hidden" value="{{ user_list.user.id }}" name="uid">
                                                </div>

                                            </div>
                                            <div class="modal-footer">
                                                <button type="button" class="btn btn-white" data-dismiss="modal">关闭
                                                </button>
                                                <button type="button" class="btn btn-primary"
                                                        onclick="user_set_passwd({{ user_list.user.id }})">保存更改
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            {% endfor %}
                            </tbody>
                        </table>

                        <div class="row">
                            <center>
                                <div class="btn-group">
                                    <a class="btn btn-white" href="{% url 'user_list' %}?page=1">首页</a>
                                    {% if page_obj.has_previous %}
                                        <a class="btn btn-white" type="button"
                                           href="?page={{ page_obj.previous_page_number }}">
                                            上一页
                                        </a>
                                    {% else %}
                                        <a class="btn btn-white" type="button" disabled="disabled">上一页</a>
                                    {% endif %}

                                    {% for page in page_range %}
                                        {% ifequal page page_obj.number %}
                                            <a class="btn btn-white active">{{ page }}</a>
                                        {% else %}
                                            <a href="?page={{ page }}" class="btn btn-white">{{ page }}</a>
                                        {% endifequal %}
                                    {% endfor %}

                                    {% if page_obj.has_next %}
                                        <a class="btn btn-white" type="button"
                                           href="?page={{ page_obj.next_page_number }}">
                                            下一页
                                        </a>
                                    {% else %}
                                        <a class="btn btn-white" type="button" disabled>下一页</a>
                                    {% endif %}
                                    <a class="btn btn-white"
                                       href="{% url 'user_list' %}?page={{ paginator.num_pages }}">末页</a>
                                </div>
                            </center>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

{% endblock %}

{% block load_js %}
    <script src="/static/js/plugins/sweetalert/sweetalert.min.js"></script>
    <script>

        function user_delete(user_id) {
            var data = {};
            data['user_id'] = user_id;
            data['csrfmiddlewaretoken'] = "{{ csrf_token }}";
            $.ajax({
                url: '{% url 'user_delete' %}',
                type: 'post',
                data: data,
                success: function (res) {
                    if (res.status == 0) {
                        swal({
                            title: res.msg,
                            type: 'success',
                            confirmButtonText: "OK"
                        }, function () {
                            window.location.reload()
                        });
                    } else {
                        swal({
                            title: res.msg,
                            type: 'error',
                            confirmButtonText: "知道了"
                        });
                    }
                }
            })
        };

        function user_stop(user_id) {
            var data = {};
            data['user_id'] = user_id;
            data['csrfmiddlewaretoken'] = "{{ csrf_token }}";
            $.ajax({
                url: '{% url 'user_stop' %}',
                type: 'post',
                data: data,
                success: function (res) {
                    if (res.status == 0) {
                        swal({
                            title: res.msg,
                            type: 'success',
                            confirmButtonText: "OK"
                        }, function () {
                            window.location.reload()
                        });
                    } else {
                        swal({
                            title: res.msg,
                            type: 'error',
                            confirmButtonText: "知道了"
                        });
                    }
                }
            })
        };

        function user_start(user_id) {
            var data = {};
            data['user_id'] = user_id;
            data['csrfmiddlewaretoken'] = "{{ csrf_token }}";
            $.ajax({
                url: '{% url 'user_start' %}',
                type: 'post',
                data: data,
                success: function (res) {
                    if (res.status == 0) {
                        swal({
                            title: res.msg,
                            type: 'success',
                            confirmButtonText: "OK"
                        }, function () {
                            window.location.reload()
                        });
                    } else {
                        swal({
                            title: res.msg,
                            type: 'error',
                            confirmButtonText: "知道了"
                        });
                    }
                }
            })
        };

        function user_set_passwd(user_id) {
            var data = {};
            data['uid'] = user_id;
            data['password_1'] = $("#" + 'pass_' + user_id + '_1').val();
            data['password_2'] = $("#" + 'pass_' + user_id + '_2').val();
            data['csrfmiddlewaretoken'] = "{{ csrf_token }}";

            $.ajax({
                url: '{% url 'user_set_password' %}',
                type: 'post',
                data: data,
                success: function (res) {
                    if (res.status == 0) {
                        swal({
                            title: res.msg,
                            type: 'success',
                            confirmButtonText: "OK"
                        }, function () {
                            window.location.reload()
                        });
                    } else {
                        swal({
                            title: res.msg,
                            type: 'error',
                            confirmButtonText: "知道了"
                        });
                    }
                }
            })
        };
    </script>
{% endblock %}